"""
先读取新加的文件
根据条件打印出来
"""
import time
from datetime import datetime


def read_latest_line(file_name):
    with open(file_name, 'r') as f:
        f.seek(0, 2)
        while True:
            line_content = f.readline()
            if not line_content:
                time.sleep(.5)
                continue
            yield line_content


def show(line_msg):
    fields = line_msg.split('\t')
    print(fields, len(fields))

    if len(fields) == 6:
        print(datetime.now())
        if (fields[3] == '瓦斯' or fields[3] == 'CO') and fields[5][0:5] == '越上限报警':
            print('警报!!! %s : %s %s 发生时间: %s ，请立即处理！' % (fields[4], fields[3], fields[5][0:-1], fields[0]))
        if (fields[3] == '瓦斯' or fields[3] == 'CO') and fields[5][0:4] == '报警解除':
            print('通告!!! %s : %s 超值危险解除，解除时间: %s' % (fields[4], fields[3], fields[0]))


if __name__ == '__main__':
    print('---开始监测---')
    file = '1.9.1.log.txt'
    lines = read_latest_line(file)
    print('lines', lines)
    for line in lines:
        print('[line]', line)
        show(line)
